/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeNodes(ListNode* head)
    {
        if (head == nullptr) return nullptr;

        ListNode* cur = head;
        ListNode* newHead = new ListNode;
        ListNode* newTail = newHead;
        int sum = 0;
        int flag = 0;

        while (cur != nullptr)
        {
            if (flag == 0 && cur->val == 0)
                flag = 1;
            else if (flag == 1 && cur->val != 0)
                sum += cur->val;
            else if (flag == 1 && cur->val == 0)
            {
                ListNode* node = new ListNode;
                node->val = sum;
                newTail->next = node;
                newTail = node;
                sum = 0;
            }
            cur = cur->next;
        }
        return newHead->next;
    }
};//